/*
 * @Author: xsh201931773442 1948130520@qq.com
 * @Date: 2022-07-05 15:23:56
 * @LastEditors: xsh201931773442 1948130520@qq.com
 * @LastEditTime: 2022-07-16 10:05:04
 * @FilePath: \financialmanagement\src\router\index.js
 * @Description:
 *
 */
import { getItem } from '@/tools/storage'
import { createRouter, createWebHashHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import MainView from '../views/MainView.vue'
import { ElMessage } from 'element-plus'
const routes = [
  {
    path: '/',
    redirect: '/home'
  },

  {
    path: '/home',
    name: '首页',
    component: MainView,
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: '银行管理',
        component: () => import('../views/layout/secondaryHome/Bankmanager.vue')
      },
      {
        path: 'umanager',
        name: '用户管理',
        component: () => import('../views/layout/secondaryHome/UManager.vue')
      },
      {
        path: 'bankInfoAdd',
        name: '银行信息新增',
        component: () => import('../views/layout/secondaryHome/BankInfoAdd.vue')
      },
      {
        path: 'userInfoAdd',
        name: '用户信息新增',
        component: () => import('../views/layout/secondaryHome/UserInfoAdd.vue')
      },
      {
        path: 'userInfoRevise',
        name: '用户信息修改',
        component: () =>
          import('../views/layout/secondaryHome/UserInfoRevise.vue')
      },
      {
        path: 'userDetails',
        name: '用户信息详情',
        component: () => import('../views/layout/secondaryHome/UserDetails.vue')
      }
    ]
  },
  {
    path: '/login',
    name: '登录',
    component: LoginView
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  //去直接放行登录
  if (to.path === '/login') return next()
  //使用路由元信息 在路由配置中加入 meta 当只有进入某个路由时，才需要验证 这样写就不需要主页面中的子页面单独去放行了
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    //如果登录状态是true，则直接next进入主页面
    if (getItem('userInfo') && getItem('userInfo').status) {
      next()
      // console.log(getItem('userInfo').status)
    } else {
      // 如果登录状态是false，那么跳转至登录页面,需要登录才能进入主页面
      ElMessage({
        message: '请登录',
        duration: 1000,
        type: 'error'
      })
      next('/login')
    }
  }
})

export default router
